const db=wx.cloud.database({})
const table=db.collection('CourseSales')

Page({

  /**
   * 页面的初始数据
   */
  data: {
  id:"",
  sale:[],
  user:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  console.log(options)
  let that=this
  that.setData({
  id:options.id
  })
  table.where({
    courseId:this.data.id
  }).get({
    success:res=>{
      this.setData({
        sale:res.data
      })
      for(var i=0;i<res.data.length;i++){
        db.collection('User').where({
          openid:res.data[i].studentID
        }).get({
          success:re=>{
            this.data.user.push(re.data[0])
       console.log(1)
       this.setData({
         user:this.data.user
       })
          }
        })
      }
    }
  })
  },

onPullDownRefresh:function(){
  let that=this
  table.where({
    courseId:this.data.id
  }).get({
    success:res=>{
      this.setData({
        sale:res.data
      })
      for(var i=0;i<res.data.length;i++){
        db.collection('User').where({
          openid:res.data[i].studentID
        }).get({
          success:re=>{
            this.data.user.push(re.data[0])
       console.log(1)
       this.setData({
         user:this.data.user
       })
          }
        })
      }
    }
  })
},

move:function(e){
  console.log(e.currentTarget.dataset.value._id)
  var _id=e.currentTarget.dataset.value._id
  wx.showModal({
    title:'提示',
    content:'确认要删除该成员吗？',
    success:function(res){
    table.doc(_id).remove({
      success:function(res){
        wx.hideLoading()
          wx.showToast({
            title: '移除成功',
          })
      },
      fail:function(res){
        wx.hideLoading()
        wx.showToast({
          title: '提交失败',
          icon:"none"
        })
      }
    })
    }
  })
},
addMember:function(){
  wx.navigateTo({
    url: '../addMember/addMember?id='+this.data.id,
  })
}
})